package com.ruoyi.quanzi.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.quanzi.mapper.SunCommentMapper;
import com.ruoyi.quanzi.domain.SunComment;
import com.ruoyi.quanzi.service.ISunCommentService;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 评论服务实现
 * 
 * @author ruoyi
 */
@Service
public class SunCommentServiceImpl implements ISunCommentService 
{
    @Autowired
    private SunCommentMapper commentMapper;

    /**
     * 查询评论列表
     * 
     * @param comment 评论信息
     * @return 评论集合
     */
    @Override
    public List<SunComment> selectSunCommentList(SunComment comment)
    {
        Long userId = null;
        try {
            userId = SecurityUtils.getUserId();
        } catch (Exception e) {
            // 未登录用户，不处理
        }
        return commentMapper.selectSunCommentList(comment, userId);
    }

    /**
     * 查询评论
     * 
     * @param id 评论ID
     * @return 评论
     */
    @Override
    public SunComment selectCommentById(Long id)
    {
        return commentMapper.selectCommentById(id);
    }

    /**
     * 查询帖子的评论列表
     * 
     * @param postId 帖子ID
     * @return 评论集合
     */
    @Override
    public List<SunComment> selectCommentsByPostId(Long postId)
    {
        Long userId = null;
        try {
            userId = SecurityUtils.getUserId();
        } catch (Exception e) {
            // 未登录用户，不处理
        }
        return commentMapper.selectCommentsByPostId(postId, userId);
    }

    /**
     * 查询用户的评论列表
     * 
     * @param uid 用户ID
     * @return 评论集合
     */
    @Override
    public List<SunComment> selectCommentsByUid(Long uid)
    {
        return commentMapper.selectCommentsByUid(uid);
    }

    /**
     * 新增评论
     * 
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int insertComment(SunComment comment)
    {
        comment.setCreateTime(new Date());
        comment.setStatus("0"); // 默认正常状态
        if (comment.getType() == null) {
            comment.setType(1); // 默认评论类型
        }
        if (comment.getParentId() == null) {
            comment.setParentId(0L); // 默认为顶级评论
        }
        return commentMapper.insertComment(comment);
    }

    /**
     * 修改评论
     * 
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int updateComment(SunComment comment)
    {
        return commentMapper.updateComment(comment);
    }

    /**
     * 删除评论
     * 
     * @param id 评论ID
     * @return 结果
     */
    @Override
    public int deleteCommentById(Long id)
    {
        // 软删除，将状态改为1
        SunComment comment = new SunComment();
        comment.setId(id);
        comment.setStatus("1");
        return commentMapper.updateComment(comment);
    }

    /**
     * 批量删除评论
     * 
     * @param ids 需要删除的评论ID
     * @return 结果
     */
    @Override
    public int deleteCommentByIds(Long[] ids)
    {
        return commentMapper.deleteCommentByIds(ids);
    }
    
    /**
     * 点赞评论
     * 
     * @param commentId 评论ID
     * @param uid 用户ID
     * @return 结果
     */
    @Override
    public int likeComment(Long commentId, Long uid)
    {
        return commentMapper.likeComment(commentId, uid);
    }
    
    /**
     * 取消点赞评论
     * 
     * @param commentId 评论ID
     * @param uid 用户ID
     * @return 结果
     */
    @Override
    public int unlikeComment(Long commentId, Long uid)
    {
        return commentMapper.unlikeComment(commentId, uid);
    }
}
